package controller;

/*
 * Action for researchers to input question for the questionnaire.
 * Used by both Binary and Network Binary type questions.
 */

import formbeans.SocioCognitiveForm;
import helper.SocioCognitiveType;
import helper.UserType;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import databeans.Question;
import databeans.SocioCognitiveq;
import databeans.User;
import model.EntityClassDAO;
import model.Model;
import model.QuestionDAO;
import model.SocioCognitiveqDAO;

public class SocioCognitiveqAction extends Action {
	private FormBeanFactory<SocioCognitiveForm> formBeanFactory = FormBeanFactory.getInstance(SocioCognitiveForm.class);
	private SocioCognitiveqDAO scqDAO;
	private QuestionDAO questionDAO;
	private EntityClassDAO ecDAO;
	
	public SocioCognitiveqAction(Model model) {
		scqDAO = model.getSocioCognitiveqDAO();
		questionDAO = model.getQuestionDAO();
		ecDAO = model.getEntityClassDAO();
	}
	
    public String getName() {
		return "sociocognitiveq.do";
	}
    
    public String perform(HttpServletRequest request) {

        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
    	// First, validate whether researcher is accessing this
    	HttpSession session = request.getSession(false);
    	User user = (User) session.getAttribute("user");
    	
    	if (user.getType() != UserType.RESEARCHER) {
    		errors.add("You are not a researcher!");
    		return "homepage.jsp";
    	}
    	
    	// Validate the question number
    	Question question = (Question) session.getAttribute("question");
    	
    	if (question == null) {
    		errors.add("Invalid question number");
    		return "createsurvey.jsp";
    	}
    	
		String button = request.getParameter("button");
		
		if (button == null)
			return "sociocognitiveq.jsp";
		
		if (button.equals("Back")) {
			synchronized(session) {
				session.setAttribute("question", null);
			}
			return "createsurvey.jsp";
		}
		
		if (button.equals("Import Entity Data")) {
			synchronized(session) {
				session.setAttribute("pageFrom", "sociocognitiveq.jsp");
				session.setAttribute("importentitybody", "init");
			}
			return "importentity.jsp";
		}

		if (button.equals("Submit")) {
	        try {
        		SocioCognitiveForm form = formBeanFactory.create(request);
            	request.setAttribute("sociocognitiveqform",form);
            	
            	if (!form.isPresent()) {
            		return "sociocognitiveq.jsp";
            	}
            	
        		// Validate for errors in the form
        		errors.addAll(form.checkSocioCognitiveFormErrors());
        		if (errors.size() != 0) {
        			return "binaryq.jsp";
        		}
        		
        		question.setQuestion(form.getQuestion());
        		question.setDynetmlAttr(form.getDynetmlAttr());
        		
        		System.out.println(form.getClassNameSource());
        		System.out.println(form.getClassNameTarget());
        		
    			question.setEntityFrom(form.getClassNameSource());
    			question.setEntityTo(form.getClassNameTarget());
    			question.setDynetmlType(-1);
    			questionDAO.updateEntityToFrom(question, ecDAO);

        		questionDAO.updateQuestion(question);
        		synchronized(session) {
        			session.setAttribute("question", question);
        		}
        		
        		SocioCognitiveq scq = new SocioCognitiveq(question.getQid());
        		
        		if (form.getAggregate().equals("Union"))
        			scq.setType(SocioCognitiveType.UNION);
        		if (form.getAggregate().equals("Average"))
        			scq.setType(SocioCognitiveType.AVERAGE);
        		if (form.getAggregate().equals("Confirmed"))
        			scq.setType(SocioCognitiveType.CONFIRMED);
        		if (form.getAggregate().equals("Locally Aggregated"))
        			scq.setType(SocioCognitiveType.LOCALLY_AGGREGATED);
        		
        		scqDAO.create(scq, questionDAO);

        		Question[] questionlist = questionDAO.getQuestionBySidSortedQnum(question.getSid(), true);
    			synchronized(session) {
    				session.setAttribute("questionlist", questionlist);
    			}
        		return "surveysummary.jsp";
	        } catch (DAOException e) {
	        	errors.add(e.getMessage());
	        	return "errors.jsp";
	        } catch (FormBeanException e) {
	        	errors.add(e.getMessage());
	        	return "errors.jsp";
	        } 
        }
        errors.add("Illegal button value of " + button);
		return "errors.jsp";
    }
}
